#pragma once

#include "Socket.hpp"
#include<sys/types.h>
#include<sys/wait.h>

const uint16_t defaultport = 8888;

class TcpServer
{
public:
    TcpServer(uint16_t port = defaultport) : _port(port)
    {
    }
    ~TcpServer()
    {
    }
    void Init()
    {
        _sock.Socket();
        _sock.Bind(_port);
        _sock.Listen();
        std::cout << "Init done..." << std::endl;
    }
    void Service(int sockfd, const std::string &clientip, const uint16_t &clientport)
    {

        char buffer[1024];
        while (true)
        {
            ssize_t n = read(sockfd, buffer, sizeof(buffer));
            if (n > 0)
            {
                buffer[n] = 0;
                std::cout << "client say# " << buffer << std::endl;
                std::string echo_string = "tcpserver echo# ";
                echo_string += buffer;

                // 读取到数据，要进行处理

                write(sockfd, echo_string.c_str(), echo_string.size());

                // std::cout<<"read error!"<<"sockfd:"<< sockfd <<std::endl;
            }
            else
            {
                break;
                close(sockfd);
            }
        }
    }
    void Start()
    {
        std::cout << "Start server" << std::endl;
        while (true)
        {
            std::string clientip;
            uint16_t clientport;
            // 这个套接字用来通信
            int sockfd = _sock.Accept(&clientip, &clientport);

            if (sockfd < 0)
            {
                continue;
            }
            std::cout << "get a new link,sockfd is :" << sockfd << std::endl;
            // Service(sockfd, clientip, clientport);


            // char buffer[1024];
            // while (true)
            // {
            //     ssize_t n = read(sockfd, buffer, sizeof(buffer));
            //     if (n > 0)
            //     {
            //         buffer[n] = 0;
            //         std::cout << "client say# " << buffer << std::endl;
            //         std::string echo_string = "tcpserver echo# ";
            //         echo_string += buffer;

            //         // 读取到数据，要进行处理

            //         write(sockfd, echo_string.c_str(), echo_string.size());

            //         // std::cout<<"read error!"<<"sockfd:"<< sockfd <<std::endl;
            //     }
            //     else
            //     {
            //         break;
                    
            //     }
            // }
            // close(sockfd);
            // pid_t id = fork();
            // if(id == 0)
            // {
            //     //子进程
            //     close(_sock.Sockfd());
            //     //子进程创建子进程，并且把子进程杀死
            //     if(fork() > 0)
            //     {
            //         exit(0);
            //     }
            //     //孙子进程执行
            //     Service(sockfd,clientip,clientport);
            //     close(sockfd);
            //     exit(0);
            // }
            // close(sockfd);
            // pid_t rid = waitpid(id,nullptr,0);
            //多线程版本
            

        }
    }

private:
    Sock _sock;
    uint16_t _port;
};